package cn.jingzhuan.element.demo.view.epoxy.models

import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import android.view.ViewGroup
import cn.jingzhuan.element.demo.view.activity.AlbumDetailActivity
import cn.jingzhuan.element.demo.view.adapter.BaseRecyclerAdapter
import cn.jingzhuan.element.demo.view.adapter.RecyclerViewHolder
import cn.jingzhuan.element.demo.view.model.AudioItem
import cn.jingzhuan.element.demo.view.util.ImageLoader
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.ItemAudioBinding
import cn.jingzhuan.element.element.databinding.LayoutHorizontalRecyclerBinding
import com.airbnb.epoxy.DataBindingEpoxyModel
import com.airbnb.epoxy.EpoxyModelClass

@EpoxyModelClass(layout = R.layout.layout_horizontal_recycler)
abstract class AudioRecyclerModel : DataBindingEpoxyModel() {

    var audioList : MutableList<AudioItem> = mutableListOf()
//    lateinit var adapter : BaseRecyclerAdapter<AudioItem>

    override fun buildView(parent: ViewGroup): View {
        if (parent == null) return super.buildView(parent)
        var view = super.buildView(parent)
        var binding = view.tag as LayoutHorizontalRecyclerBinding
        Log.i("ELEMNT >>>>>>","Audio List: ${audioList.size}")
        var adapter = object : BaseRecyclerAdapter<AudioItem>(parent.context,audioList) {
            override fun getItemLayoutId(viewType: Int): Int = R.layout.item_audio

            override fun bindData(holder: RecyclerViewHolder, position: Int, item: AudioItem) {
                var audioImageView = holder.getImageView(R.id.audio_image)
                ImageLoader.loadImage(audioImageView,item.image)
                holder.getTextView(R.id.audio_title).text = item.title
                holder.getImageView(R.id.audio_listen_ico).setImageResource(R.mipmap.icon_listener)
                holder.getTextView(R.id.audio_listen).text = item.listen.toString()
                holder.getView().setOnClickListener {
                    AlbumDetailActivity.start(holder.getView().context, item.id)
                }
//                var binding = holder.getView().tag as ItemAudioBinding
//                binding.audioTitle.text = item.title
//                binding.audioListenIco.setImageResource(R.mipmap.icon_listener)
//                binding.autoListen.text = item.listen.toString()
            }
        }
        var linearLayoutManager = LinearLayoutManager(parent.context)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        binding.recyclerView.layoutManager  = linearLayoutManager
 //        val decoration = HorizontalSpacesItemDecoration(DisplayUtils.dip2px(parent.context,15f))
//        binding?.recyclerView?.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter

        return view
    }
}
